package edu.sjtu.grid.workflow.group;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import edu.sjtu.grid.workflow.facts.Job;
import edu.sjtu.grid.workflow.facts.Mapping;
import edu.sjtu.grid.workflow.facts.Run;

public class GroupMapping {

	private Run run = null;
	private GroupNode groupNode = null;
	private Map<Job, Mapping> jobToMapping = null;
	private List<Mapping> mappingList = null;
	
	public GroupMapping(Run run, GroupNode groupNode, List<Mapping> mappingList)
	{
		this.run = run;
		this.groupNode = groupNode;
		this.mappingList = mappingList;
		this.jobToMapping = new HashMap<Job, Mapping>();
		for(Mapping map : this.mappingList)
			this.jobToMapping.put(map.getJob(), map);
	}
	
	public GroupMapping(Run run, GroupNode groupNode, Mapping map)
	{
		this.run = run;
		this.groupNode = groupNode;
		this.jobToMapping = new HashMap<Job, Mapping>();
		this.jobToMapping.put(map.getJob(), map);
		this.mappingList = new ArrayList<Mapping>();
		this.mappingList.add(map);
	}
	
	public double getToTime()
	{
		Job last = groupNode.getLastJob();
		Mapping map = jobToMapping.get(last);
		return map.getToTime();
	}
	
	public double getCost()
	{
		double sum = 0;
		Job job = this.groupNode.getFirstJob();
		sum += this.jobToMapping.get(job).getRealTransAndComputationCost(run);
		while(!this.groupNode.isLastJob(job))
		{
			Job job2 = this.groupNode.getNextJob(job);
			sum += this.jobToMapping.get(job).getRealTransAndComputationCost(this.jobToMapping.get(job2));
			job = job2;
		}

		return sum;
	}
	
	public List<Mapping> getMappingList()
	{
		return this.mappingList;
	}
}
